(* added to Subversion server *)
(* created by Sweepy *)
(* class - manage A* algorithm *)



(* return the node having the lowest cost value *)
let minNode l =
	let rec findMin l min found = match l with
		| [] -> found
		| e::l -> let (_,_,cout,_,_) = e in
			(if (cout < min) then (findMin l cout e)
			else (findMin l min found))
	in findMin l 0 (0,0,0,0,0)


(* get the color cost of a specific cell *)
let getColorCost x y l = l.(x).(y)


(* return the absolute value of an integer *)
let absoluteValue a = if (a < 0) then (-a) else a


(* return the heuristic distance between two points *)
let getDistance (x1,y1) (x2,y2) = absoluteValue(x1 - x2) +
				absoluteValue (y1 - y2)

(* delete a specific element from a list *)
let deletePoint x l =
	let rec deleting lst newLst = match lst with
		| [] -> newLst
		| e::lst -> if (e == x) then
			deleting lst newLst
			else
			deleting lst (e::newLst)
	in deleting l []


(* return whether a specific element is present in an enclosed list *)
let rec findElement x l = match l with
	| [] -> false
	| e::l -> if (e == x) then true else (findElement x l)


class aStar =
	object (self)
		val mutable mapPixels = Array.make_matrix 1 1 0
		val mutable mapSolved = Array.make_matrix 1 1 0
		val mutable imageWidth = 0
		val mutable imageHeight = 0
		val mutable departurePoint = (0,0)
		val mutable arrivalPoint = (0,0)

		method set_departurePoint x = departurePoint <- x
		method set_arrivalPoint x = arrivalPoint <- x

		method set_mapPixels_size w h =
			imageWidth <- w;
			imageHeight <- h;
			mapPixels <- Array.make_matrix w h 0

		method set_mapPixels_color x y color =
			mapPixels.(x).(y) <- color

		method find_best_path () =
			let (x,y) = departurePoint in
				let open_list = (x,y,0,x,y)::[] and
				closed_list = [] in
			while (List.length open_list > 0) do
				begin (* while loop *)

				let x = (minNode open_list) in
				begin

				let (a,b,cost,pX,pY) = x and gscore = 0 in
				if ((a,b) == arrivalPoint) then
					print_endline "path found !";
					();

				open_list = (deletePoint x open_list);
				closed_list = x::closed_list;

				for i = -1 to 1 do
					for j = -1 to 1 do
						begin


					if (i != 0 && j != 0) then
						begin
						(*let (xC,yC,_,_,_) = x in
						if (findElement (xC + i,yC + j,0,0,0) closed_list) then
							gscore = 4;
							print_endline "lol"*)
						end


						end
					done;
				done;


				end

				end
			done


	end;;
